//
//  UITableView+ZJ.swift
//  ZJSwiftKit
//
//  Created by 左潇 on 2023/6/30.
//

import Foundation


// MARK: - Methods
public extension ZJPrefix where Base: UITableView {
    /// 检查IndexPath在tableView中是否有效。
    ///
    /// - Parameter indexPath: An IndexPath to check.
    /// - Returns: Boolean value for valid or invalid IndexPath.
    func isValidIndexPath(_ indexPath: IndexPath) -> Bool {
        return indexPath.section >= 0 &&
        indexPath.row >= 0 &&
        indexPath.section < base.numberOfSections &&
        indexPath.row < base.numberOfRows(inSection: indexPath.section)
    }
    
    /// 安全滚动到可能无效的IndexPath。
    ///
    /// - Parameters:
    ///   - indexPath: Target IndexPath to scroll to.
    ///   - scrollPosition: Scroll position.
    ///   - animated: Whether to animate or not.
    func safeScrollToRow(at indexPath: IndexPath, at scrollPosition: UITableView.ScrollPosition, animated: Bool) {
        guard indexPath.section < base.numberOfSections else { return }
        guard indexPath.row < base.numberOfRows(inSection: indexPath.section) else { return }
        base.scrollToRow(at: indexPath, at: scrollPosition, animated: animated)
    }
}

